//Added for the mail module
function select_mails(type)
{
//this function assumes that only two types of checkboxes will be available 
// 1)unread_check 2)read_check 
//making everything unchecked before checking any checkbox
	$('.unread_check').attr('checked', false);
	$('.read_check').attr('checked', false);
	switch(type)
	{
		case 'none':
		{
			$('.unread_check').attr('checked', false);
			$('.read_check').attr('checked', false);
			break;
		}
		case 'all':
		{
			$('.unread_check').attr('checked', true);
			$('.read_check').attr('checked', true);
			break;
		}
		case 'unread':
		{
			$('.unread_check').attr('checked', true);
			break;
		}
		case 'read':
		{
			$('.read_check').attr('checked', true);
			break;
		}
	}
	
}
function validInput(string)
{
	re=/[<>].*/;
   return re.test(string);
}
function perform_search_action(action,check_valid,query_string,submitnow)
{
	var valid=true;
	if(check_valid==true)
	{
		var s_val = $('#seacrh_m').val();
		var  re=/[<>].*/;
		if(re.test(s_val))
		{
			valid = false;
		}
		if(valid==false)
		{
			alert("Please enter valid word to search.");
			return false;
		}
	}
	switch(action)
	{
		case 'ssearch':
		{
			$("#mail_box_frm1").attr("action","/mail/sent/index/"+query_string);
			break;
		}
		case 'tsearch':
		{
			$("#mail_box_frm1").attr("action","/mail/trash/index/"+query_string);
			break;
		}
		default:
		{
			$("#mail_box_frm1").attr("action","/mail/inbox/index/"+query_string);
			break;
		}
	}
	if(submitnow)
	{
		$("#mail_box_frm1").submit();
		return true;
	}
	return true;
	
}
function perform_action(action,check_valid,query_string)
{
	atleast_one = false;
	if(check_valid==true)
	{
		if($('.unread_check').is(':checked'))
		{
			atleast_one = true;
		}
		if($('.read_check').is(':checked'))
		{
			atleast_one = true;
		}
		if(atleast_one==false)
		{
			alert("Please select at least one message.");
			return false;
		}
	}
	switch(action)
	{
		case 'delete':
		{
			$("#mail_box_frm").attr("action","/mail/inbox/delete/"+query_string);
			break;
		}
		case 'read':
		{
			$("#mail_box_frm").attr("action","/mail/inbox/read/"+query_string);
			break;
		}
		case 'unread':
		{
			$("#mail_box_frm").attr("action","/mail/inbox/unread/"+query_string);
			break;
		}
		case 'inbox':
		default:
		{
			$("#mail_box_frm").attr("action","/mail/inbox/index/"+query_string);
			break;
		}
		case 'sent':
		{
			$("#mail_box_frm").attr("action","/mail/sent/index/"+query_string);
			break;
		}
		case 'trash':
		{
			$("#mail_box_frm").attr("action","/mail/trash/index/"+query_string);
			break;
		}
		case 'sdelete':
		{
			$("#mail_box_frm").attr("action","/mail/sent/delete/"+query_string);
			break;
		}
		
		case 'tdelete':
		{
			$("#mail_box_frm").attr("action","/mail/trash/delete/"+query_string);
			break;
		}
	}
	$("#mail_box_frm").submit();
}

function recipientAlreadyAdded(recpName) {
	var val = str_trim($('#txtto').val());
	val = str_trim(val,",");
	val = str_trim(val,";");
	if(val.length == 0) //no recp
		return false;
	else if(val.indexOf(",") == -1) // in case that there is only 1 recp
		return val.toLowerCase().indexOf(recpName.toLowerCase()) != -1
	else 
		return !(val.toLowerCase().search(new RegExp(""+recpName.toLowerCase()+",","mgi")) == -1 && val.toLowerCase().search(new RegExp(""+recpName.toLowerCase()+";","mgi")) == -1 && 
		val.toLowerCase().search(new RegExp(""+recpName.toLowerCase()+"$","mgi")) == -1); 
}
function addMultipleRecipients(recps) { 
	var recps = recps.split(",");
	for(var i =0;i<recps.length;i++) {
		if(recipientAlreadyAdded(recps[i]))
			continue;
		else {
			addToRecipients(recps[i]);
		}
	}
	return false; //a href="#", stop event bubblin'
}
function removeFromRecipients(recpName) {
	if(recpName.length == 0)
		return false;
	
	var val = $('#txtto').val();
	val = val.replace(new RegExp(""+recpName.toLowerCase()+",","mgi"),"");
	val = val.replace(new RegExp(""+recpName.toLowerCase()+";","mgi"),"");
	val = val.replace(new RegExp(""+recpName.toLowerCase()+"$","mgi"),"");
	val = str_trim(val);
	val = str_trim(val,",");
	val= str_trim(val,';');
	$('#txtto').val(val);
	return false;
}
function removeMultipleRecipients(recps) {
	var recps1 = recps.split(",");
	for(var i =0;i<recps1.length;i++) {
		if(recipientAlreadyAdded(recps1[i])) {
			removeFromRecipients(recps1[i]);
		}
	}
	return false; //a href="#", stop event bubblin'
}
function str_trim(str, char) {
	if(typeof(char) == "undefined")
		return str.replace(/^\s*/, "").replace(/\s*$/, "");
	else
		return str.replace(new RegExp("^"+char+"*","g"), "").replace(new RegExp(char + "*$","g"), "");
}
function addToRecipients(name) {
	if(recipientAlreadyAdded(name))
		return false;
	
	var obj = $('#txtto');

	var val= str_trim(obj.val())
val= str_trim(val,';')
val= str_trim(val,',')
	if (val.length == 0)
		val = name
	else
		val = val + ", " + name

	obj.val(val)

	return false
}
function clearattachment()
{
	 $('#fileContainerDiv').html($('#fileContainerDiv').html());
	//$('#upload1').val('');
}
function printMailFlash(msg, classname) { 
	$('#mailflashes').append('<div class="flash ' + classname + '">' + msg + '</div>');
}
function trackPrint(msg_id)
{
	$.ajax({
			type: "POST",
			url: "/mail/view/print-track/",
			data: "id="+msg_id,
			success: function(mesg)
			{
				
			}
		});
window.print();

}